package org.xx.armory.swing;

import javax.swing.*;
import java.awt.Dimension;
import java.awt.Point;

import static org.apache.commons.lang3.StringUtils.split;
import static org.apache.commons.lang3.StringUtils.trimToEmpty;
import static org.apache.commons.lang3.math.NumberUtils.toInt;

/**
 * 框架窗体的位置属性，包括位置、尺寸和状态。
 */
public final class FrameBounds {
    private final int status;
    private final int left;
    private final int top;
    private final int width;
    private final int height;

    /**
     * 构造位置属性对象。
     *
     * @param status
     *         窗体状态。
     * @param top
     *         窗体顶部位置。
     * @param left
     *         窗体左侧位置。
     * @param width
     *         窗体的宽度。
     * @param height
     *         窗体的高度。
     * @see JFrame#getExtendedState()
     */
    public FrameBounds(
            int status,
            int left,
            int top,
            int width,
            int height
    ) {
        this.status = status != JFrame.ICONIFIED ? status : JFrame.NORMAL;
        this.left = left;
        this.top = top;
        this.width = width;
        this.height = height;
    }

    public FrameBounds(
            String s
    ) {
        s = trimToEmpty(s);

        String[] parts = split(s, ",");
        int status = 0;
        int top = 0;
        int left = 0;
        int width = 0;
        int height = 0;
        if (parts.length > 0) {
            status = toInt(parts[0]);
        }
        if (parts.length > 1) {
            left = toInt(parts[1]);
        }
        if (parts.length > 2) {
            top = toInt(parts[2]);
        }
        if (parts.length > 3) {
            width = toInt(parts[3]);
        }
        if (parts.length > 4) {
            height = toInt(parts[4]);
        }

        this.status = status != JFrame.ICONIFIED ? status : JFrame.NORMAL;
        this.top = top;
        this.left = left;
        this.width = width;
        this.height = height;
    }

    public final Point getLocation() {
        return new Point(this.left, this.top);
    }

    public final Dimension getSize() {
        return new Dimension(this.width, this.height);
    }

    public final int getStatus() {
        return this.status;
    }

    public final int getTop() {
        return this.top;
    }

    public final int getLeft() {
        return this.left;
    }

    public final int getWidth() {
        return this.width;
    }

    public final int getHeight() {
        return this.height;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final String toString() {
        return this.status + "," + this.left + "," + this.top + "," + this.width + "," + this.height;
    }
}
